package org.ihelpuoo.localize;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import org.ihelpuoo.io.FileUtils;
import org.ihelpuoo.io.StreamUtils;
import org.ihelpuoo.io.StringBufferOutputStream;
import org.ihelpuoo.lang.StringUtils;

public class Localize
{
	public static TimeZone getTimeZone(int off)
	{
		if (off == 0)
			return TimeZone.getTimeZone("GMT");
		else if (off > 0)
			return TimeZone.getTimeZone("GMT+" + off);
		return TimeZone.getTimeZone("GMT" + off);
	}

	public static TimeZone getTimeZone(Locale locale)
	{
		Calendar c = Calendar.getInstance(locale);
		return c.getTimeZone();
	}

	public static String convertAscii2Native(CharSequence s)
	{
		if (null == s)
			return null;
		boolean prevIsEscapeChar = false;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);
			if (c == '\\')
			{
				if (prevIsEscapeChar)
				{
					sb.append(c);
					prevIsEscapeChar = false;
				}
				else
					prevIsEscapeChar = true;
			}
			else if (c == 'u' && prevIsEscapeChar)
			{
				prevIsEscapeChar = false;
				StringBuffer code = new StringBuffer();
				for (int j = 0; j < 4; j++)
				{
					char b = s.charAt(++i);
					code.append(b);
				}
				c = (char)Integer.parseInt(code.toString(),16);
				sb.append(c);
			}
			else
				sb.append(c);
		}
		return sb.toString();
	}

	public static void native2ascii(File src, File dest, String encoding) throws IOException
	{

		BufferedReader r = new BufferedReader((null == encoding ? new InputStreamReader(
				new FileInputStream(src))
				: new InputStreamReader(new FileInputStream(src),encoding)));
		if (dest.exists())
			FileUtils.deleteFile(dest);
		BufferedWriter w = new BufferedWriter(new FileWriter(dest));
		int c;
		while (-1 != (c = r.read()))
		{
			writeChar(w,c);
		}
		r.close();
		w.close();
	}

	public static String convertNative2Ascii(CharSequence s) throws IOException
	{
		if (null == s)
			return null;
		StringBuffer sb = new StringBuffer();
		OutputStream ops = new StringBufferOutputStream(sb);
		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);
			writeChar(ops,c);
		}
		return sb.toString();
	}

	private static void writeChar(Writer w, int c) throws IOException
	{
		if (c > 0 && c < 256)
		{
			w.write((char)c);
		}
		else if (c > 256)
		{
			w.write("\\u");
			w.write(StringUtils.fillHex(c,4).toString());
		}
		else
		{
			w.write("-\\u");
			w.write(StringUtils.fillHex(c,4).toString());
		}
	}

	private static void writeChar(OutputStream ops, int c) throws IOException
	{
		if (c > 0 && c < 256)
		{
			ops.write(c);
		}
		else if (c > 256)
		{
			StreamUtils.write(ops,new StringBuffer("\\u").append(StringUtils.fillHex(c,4)));
		}
		else
		{
			StreamUtils.write(ops,new StringBuffer("-\\u").append(StringUtils.fillHex(c,4)));
		}
	}

	public static void convertEncoding(File src, String srcEncoding, File dest, String destEncoding)
			throws IOException
	{
		BufferedReader r = new BufferedReader((null == srcEncoding ? new InputStreamReader(
				new FileInputStream(src)) : new InputStreamReader(new FileInputStream(src),
				srcEncoding)));
		if (dest.exists())
			FileUtils.deleteFile(dest);
		BufferedWriter w = new BufferedWriter(null == destEncoding ? new OutputStreamWriter(
				new FileOutputStream(dest)) : new OutputStreamWriter(new FileOutputStream(dest),
				destEncoding));
		int c;
		while (-1 != (c = r.read()))
			w.write(c);
		r.close();
		w.close();
	}
}
